package com.spring.sunflower.userinfo;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.spring.sunflower.MainActivity;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.common.CommonContract;
import com.spring.sunflower.constant.Constant;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/6/28<br/>
 * <p>
 * Describe:<br/>
 */
public class HobbyActivity extends BaseActivity<HobbyPresenter> implements CommonContract.HobbyView, OnItemClickListener {
    private RecyclerView mRecyclerView;
    private TextView tvDone;
    private HobbyAdapter mAdapter;
    private List<LabelBean.DatasBean> mData = new ArrayList<>();
    private List<String> hobbyData = new ArrayList<>();
    private String genderType;
    private GradientDrawable tvDoneBackground;

    @Override
    public void initView() {
        Intent intent = getIntent();
        genderType = intent.getStringExtra(Constant.KEY_GENDER_TYPE);
        String femaleNickname = getField(Constant.KEY_REGISTER_NICKNAME_FEMALE, "");
        String nickname = getField(Constant.KEY_REGISTER_NICKNAME, "");
        tvTitle.setText("个性爱好");
        mRecyclerView = findViewById(R.id.rvRecyclerView);
        tvDone = findViewById(R.id.tvDone);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new HobbyAdapter(R.layout.adapter_item_hobby, mData);
        mRecyclerView.setAdapter(mAdapter);
        mPresenter.getHobbyType();
        mAdapter.setOnItemClickListener(this);
        tvDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hobbyData.size() > 0) {
                    String string = handleHobbyData();
                    if (Constant.GENDER_MALE.equals(genderType)) {
                        mPresenter.register(genderType, string, nickname);
                    } else {
                        mPresenter.register(genderType, string, femaleNickname);
                    }
                }
            }
        });
        tvDoneBackground = (GradientDrawable) tvDone.getBackground();
    }

    @NotNull
    private String handleHobbyData() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < hobbyData.size(); i++) {
            if (i < hobbyData.size() - 1) {
                stringBuffer.append(hobbyData.get(i) + ",");
            } else {
                stringBuffer.append(hobbyData.get(i));
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_hobby;
    }

    @Override
    public HobbyPresenter setPresenter() {
        return new HobbyPresenter(this);
    }

    @Override
    public void showHobby(List<LabelBean.DatasBean> data) {
        mAdapter.addData(data);
    }

    @Override
    public void registerSucceed() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void registerFailed(int status, String msg) {
        showToast("状态码：" + status + ",原因：" + msg);
    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        TextView textView = (TextView) adapter.getViewByPosition(position, R.id.tvLabel);
        boolean enabled = textView.isEnabled();
        textView.setEnabled(!enabled);
        LabelBean.DatasBean datasBean = mData.get(position);
        GradientDrawable background = (GradientDrawable) textView.getBackground();
        if (!enabled) {
            background.setColor(Color.parseColor("#" + datasBean.getColor()));
            hobbyData.add(datasBean.getId());
        } else {
            background.setColor(ContextCompat.getColor(this, R.color.gray_4));
            int index = hobbyData.indexOf(datasBean.getId());
            if (index != -1) {
                hobbyData.remove(index);
            }
        }
        if (hobbyData.size() > 0) {
            tvDoneBackground.setColor(Color.parseColor("#FADB4A"));
        } else {
            tvDoneBackground.setColor(Color.parseColor("#CCCCCC"));
        }
    }
}
